package org.diffshop.product.service.impl;

import org.diffshop.product.vo.AttrAttrgroupRelationVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.diffshop.common.utils.PageUtils;
import org.diffshop.common.utils.Query;

import org.diffshop.product.dao.PmsAttrAttrgroupRelationDao;
import org.diffshop.product.entity.PmsAttrAttrgroupRelationEntity;
import org.diffshop.product.service.PmsAttrAttrgroupRelationService;


@Service("pmsAttrAttrgroupRelationService")
public class PmsAttrAttrgroupRelationServiceImpl extends ServiceImpl<PmsAttrAttrgroupRelationDao, PmsAttrAttrgroupRelationEntity> implements PmsAttrAttrgroupRelationService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<PmsAttrAttrgroupRelationEntity> page = this.page(
                new Query<PmsAttrAttrgroupRelationEntity>().getPage(params),
                new QueryWrapper<PmsAttrAttrgroupRelationEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 删除属性和分组关系
     *
     * @param relationVOs
     */
    @Override
    public void deleteAttrAttrgroupRelation(AttrAttrgroupRelationVO[] relationVOs) {
        if (relationVOs != null && relationVOs.length > 0) {
            List<PmsAttrAttrgroupRelationEntity> entityList = Arrays.stream(relationVOs).map(relationVO -> {
                PmsAttrAttrgroupRelationEntity relationEntity = new PmsAttrAttrgroupRelationEntity();
                BeanUtils.copyProperties(relationVO, relationEntity);
                return relationEntity;
            }).collect(Collectors.toList());
            // 批量删除
            baseMapper.deleteBatch(entityList);
        }
    }

    /**
     * 新建组和属性关联
     * @param relationVO
     */
    @Override
    public void saveBatch(AttrAttrgroupRelationVO[] relationVO) {
        List<PmsAttrAttrgroupRelationEntity> collect = Arrays.stream(relationVO).map(relation -> {
            PmsAttrAttrgroupRelationEntity relationEntity = new PmsAttrAttrgroupRelationEntity();
            BeanUtils.copyProperties(relation, relationEntity);
            return relationEntity;
        }).collect(Collectors.toList());

        this.saveBatch(collect);
    }

}
